package com.thread.future;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import com.thread.utility.ThreadUtility;

public class CompletableFutureTask {
	public static void main(String[] args) {
		
		List<CompletableFuture<String>> futureList = new ArrayList<CompletableFuture<String>>();
		
		//开启异步线程任务，有返回值
		CompletableFuture<String> firstFuture = CompletableFuture.supplyAsync(() -> {
			System.out.println("firstFuture begin");
			ThreadUtility.sleep(6);
			System.out.println("firstFuture end");
			return "firstFuture";
		});
		futureList.add(firstFuture);
		
		
		CompletableFuture<String> secondFuture = CompletableFuture.supplyAsync(() -> {
			System.out.println("secondFuture begin");
			ThreadUtility.sleep(3);
			System.out.println("secondFuture end");
			return "secondFuture";
		});
		futureList.add(secondFuture);
		
		
		List<String> resultList = futureList.stream().map(CompletableFuture::join).collect(Collectors.toList());
		System.out.println(resultList);
	}
}
